package com.lzw.breezeNovel.webApp.service.impl.video;

import com.lzw.breezeNovel.model.entity.pojo.video.Anime;
import com.lzw.breezeNovel.model.entity.query.PaginationResultQuery;
import com.lzw.breezeNovel.model.enums.viedo.AnimeStatus;
import com.lzw.breezeNovel.webApp.mapper.video.AnimeMapper;
import com.lzw.breezeNovel.webApp.service.video.AnimeService;
import com.lzw.breezeNovel.webApp.vo.video.AnimeVo;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author 清风明月
 * @Package com.lzw.breezeNovel.webApp.service.impl.video
 * @date 2025/3/18
 * @description 动漫
 */
@Slf4j
@Service
@SuppressWarnings("all")
public class AnimeServiceImpl implements AnimeService {

    /**
     * 注入mapper
     */
    @Autowired
   private AnimeMapper animeMapper;

    /**
     * @param pageNo
     * @param size
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public PaginationResultQuery<AnimeVo> getAnime(Integer pageNo, Integer size) {
        //计算分页数据
        int startPage = startPage(pageNo, size);
        //查询数据
        Long count = animeMapper.selectCount();
        //判断是否有数据
        if (count == 0) {
            return new PaginationResultQuery<>(0, size, pageNo, 0, Collections.emptyList());
        }
        //分页获取数据
        List<Anime> animeList = animeMapper.selectAnime(startPage, size);
        //拷贝数据
        List<AnimeVo> animeVoList = Copy(animeList);
        return new PaginationResultQuery<AnimeVo>(count.intValue(), size, pageNo, animeVoList);
    }

    /**
     * 获取相关动漫
     *
     * @param type
     * @param videoId
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public List<AnimeVo> getRelatedAnime(String type, Integer videoId) {
        //判断动漫类型是否为空
        if (type == null) {
            return List.of();
        }
        //查询数据
        List<Anime> animeList = animeMapper.selectRelatedAnime(type, videoId);
        //拷贝数据
        return Copy(animeList);
    }

    /**
     * 拷贝数据
      * @param animeList
     * @return
     */
    @Nullable
    @Contract(pure = true)
    private List<AnimeVo> Copy(List<Anime> animeList) {
        //判断是否为空
        if (animeList == null || animeList.size() == 0) {
            return null;
        }
        return animeList.stream()
                .map(anime -> {
                    AnimeVo animeVo = new AnimeVo();
                    BeanUtils.copyProperties(anime, animeVo);
                    //获取动漫状态
                    Integer status = anime.getStatus();
                    if(AnimeStatus.SERIAL.getCode() == status){
                        animeVo.setStatus(AnimeStatus.SERIAL.getDesc());
                    }else
                    {
                        animeVo.setStatus(AnimeStatus.FINISH.getDesc());
                    }
                    return animeVo;
                })
                .collect(Collectors.toList());
    }

    //计算分页数据
    public int startPage(Integer pageNo, Integer size){
        //判断分页数据是否为空
        pageNo = Optional.of(pageNo).orElse(1);
        size = Optional.of(size).orElse(10);
        pageNo = Math.max(pageNo, 1);
        size = Math.max(size, 1);
        //计算分页
        pageNo = (pageNo - 1) * size;
        return pageNo;
    }
}
